/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import pojo.Binhluan;
import pojo.Danhsachsanpham;
import pojo.Nguoidung;
import pojo.Sanpham;
import until.HibernateUtil;

/**
 *
 * @author Alvis
 */
public class SanPhamDao {
    Session session;

    public SanPhamDao() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    public void Insert(Sanpham value) {
        try {
            session.getTransaction().begin();
            String sql1 = "select nextval('sanpham_masanpham_seq')";
            Statement st = session.connection().createStatement();
            ResultSet rq = (ResultSet) st.executeQuery(sql1);
            int ma = 0;
            while (rq.next()) {
                ma = rq.getInt("nextval");
            }
            value.setMasanpham(ma);
            session.save(value);


            session.flush();
            session.getTransaction().commit();
        } catch (SQLException ex) {
            Logger.getLogger(NguoiDungDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        

    }
    public void Update(Sanpham value, int id) {
        session.getTransaction().begin();
        Sanpham valueTemp = (Sanpham) session.get(Sanpham.class, id);
        valueTemp.setTensanpham(value.getTensanpham());
        valueTemp.setXuatxu(value.getXuatxu());
        valueTemp.setGiaban(value.getGiaban());
        valueTemp.setLoaisanpham(value.getLoaisanpham());
        valueTemp.setMota(value.getMota());
        valueTemp.setNhasanxuat(value.getNhasanxuat());
        valueTemp.setSoluotban(value.getSoluotban());
        valueTemp.setSoluotxem(value.getSoluotxem());
        session.update(valueTemp);
        // session.flush();
        session.getTransaction().commit();
    }
    public void Delete(int id) {
        session.getTransaction().begin();
        String sql = "from Binhluan where masanpham=?";
        Query query = session.createQuery(sql);
        query.setInteger(0, id);
        List result = query.list();
        Binhluan[] listBinhLuan = new Binhluan[result.size()];
        result.toArray(listBinhLuan);
        for (int i = 0; i < listBinhLuan.length; i++) {
            session.delete(session.get(Binhluan.class, listBinhLuan[i].getMabinhluan()));
        }
        sql = "from Danhsachsanpham where masanpham=?";
        query = session.createQuery(sql);
        query.setInteger(0,id);
        result = query.list();
        Danhsachsanpham[] listDanhSachSanPham = new Danhsachsanpham[result.size()];
        result.toArray(listDanhSachSanPham);
        for (int i = 0; i < listDanhSachSanPham.length; i++) {
            session.delete(session.get(Danhsachsanpham.class, listDanhSachSanPham[i].getId()));
        }
        
        session.delete(session.get(Sanpham.class, id));
        session.flush();
        session.getTransaction().commit();
    }
    public Sanpham[] getAllProduct() {
        session.getTransaction().begin();
        String sql = "from Sanpham";
        Query query = session.createQuery(sql);
        List result = query.list();
        Sanpham[] listvalue = new Sanpham[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
    public Sanpham[] SearchProduct(String name) {
        session.getTransaction().begin();
        String sql = "from Sanpham where tensanpham like ?";
        Query query = session.createQuery(sql);
        query.setString(0,"%"+ name +"%");
        List result = query.list();
        Sanpham[] listvalue = new Sanpham[result.size()];
        result.toArray(listvalue);
        return listvalue;
    }
    public Sanpham getProduct(int id) {

        session.getTransaction().begin();
        String sql = "from Sanpham where masanpham=?";
        Query query = session.createQuery(sql);

        query.setInteger(0,id);
        Sanpham result = (Sanpham) query.uniqueResult();
        session.flush();
        session.getTransaction().commit();
        return result;

    }
    
}
